<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Subcriptionoptions extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('subcriptionoption');
	}
	public function ajax_subcriptionoption_get(){
		$subcriptionoptionEntity =  new CI_Subcription_option_entity();
		$subcriptionoption = $subcriptionoptionEntity->getSubcriptionOptionByCode($_REQUEST['code']);
		echo json_encode($subcriptionoption);
	}
	public function ajax_subcriptionoption_list(){
		$subcriptionoptionEntity =  new CI_Subcription_option_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$subcriptionoptionList = $subcriptionoptionEntity->getSubcriptionOptionListByLanguage($defaultLanguageId);
		$data['subcriptionoptionList'] = $subcriptionoptionList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/subcriptionoptions/index', $data);
	}
	public function ajax_subcriptionoption_save(){
		$subcriptionoptionNameList = $_REQUEST['subcriptionoption'];
		$subcriptionoptionPriceList = $_REQUEST['subcriptionoption_price'];
		
		$subcriptionoptionNameDefault = '';
		foreach ($subcriptionoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$subcriptionoptionNameDefault = $value;
				break;
			}
		}
		
		$subcriptionoptionPriceDefault = 0;
		foreach ($subcriptionoptionPriceList as $key => $value) {
			if($value != null && (is_int($value) || is_float($value))){
				$subcriptionoptionPriceDefault = $value;
				break;
			}
		}
		
		$result = array();
		if($subcriptionoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_subcriptionoption_require');
		}else{
			$code = 0;
			foreach ($subcriptionoptionNameList as $key => $value) {
				$subcriptionoptionEntity =  new CI_Subcription_option_entity();
				if($value != null && $value != ''){
					$subcriptionoptionEntity->setOption($value);
				}else{
					$subcriptionoptionEntity->setOption($subcriptionoptionNameDefault);
				}
				
				if($subcriptionoptionPriceList[$key]){
					$subcriptionoptionEntity->setPrice(floatval($subcriptionoptionPriceList[$key]));
				}else{
					$subcriptionoptionEntity->setPrice($subcriptionoptionPriceDefault);
					
				}
				
				$subcriptionoptionEntity->setLanguageId($key);
				$subcriptionoptionEntity->setCode($code);
				$id = $subcriptionoptionEntity->save();
				if($code == 0){
					$code = $id;
					$subcriptionoptionEntity->setId($id);
					$subcriptionoptionEntity->setCode($code);
					$subcriptionoptionEntity->update();
				}
			}
			$result['result'] = true;
			$result['message'] =lang('escort_subcriptionoption_save_success');
			$result['id'] =$id;
		}
		echo json_encode($result);
	}
	public function ajax_subcriptionoption_update(){
		$subcriptionoptionNameList = $_REQUEST['subcriptionoption'];
		$subcriptionoptionIdList = $_REQUEST['subcriptionoption_id'];
		$subcriptionoptionPriceList = $_REQUEST['subcriptionoption_price'];
		$subcriptionoptionCode = $_REQUEST['subcriptionoption_code'];
		$subcriptionoptionNameDefault = '';
		foreach ($subcriptionoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$subcriptionoptionNameDefault = $value;
				break;
			}
		}
		$subcriptionoptionPriceDefault = 0;
		foreach ($subcriptionoptionPriceList as $key => $value) {
			if($value != null && (is_int($value) || is_float($value))){
				$subcriptionoptionPriceDefault = $value;
				break;
			}
		}
		$result = array();
		if($subcriptionoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_subcriptionoption_require');
		}else{
			foreach ($subcriptionoptionNameList as $key => $value) {
				$subcriptionoptionEntity =  new CI_Subcription_option_entity();
				if($value != null && $value != ''){
					$subcriptionoptionEntity->setOption($value);
				}else{
					$subcriptionoptionEntity->setOption($subcriptionoptionNameDefault);
				}
				
				if($subcriptionoptionPriceList[$key]){
					$subcriptionoptionEntity->setPrice(floatval($subcriptionoptionPriceList[$key]));
				}else{
					$subcriptionoptionEntity->setPrice($subcriptionoptionPriceDefault);
				}
				$subcriptionoptionEntity->setLanguageId($key);
				$subcriptionoptionEntity->setCode($subcriptionoptionCode);
				$subcriptionoptionEntity->setId($subcriptionoptionIdList[$key]);
				$subcriptionoptionEntity->update();
			}
			$result['result'] = true;
			$result['message'] =lang('escort_subcriptionoption_save_success');
		}
		
		echo json_encode($result);
	}
	public function ajax_subcriptionoption_delete(){
		$result = array();
		$subcriptionoptionEntity =  new CI_Subcription_option_entity();
		$result['result'] = $subcriptionoptionEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_subcriptionoption_delete_success');
		}else{
			$result['message'] = lang('escort_subcriptionoption_delete_error');
		}
		echo json_encode($result);
	}
}
?>